<?php

class App_mod_trunks extends MY_Model {

function __construct()
{
	parent::__construct();
}

public function get_trunks_by_carrier($carrier_id,$trunk_type,$status = false)
{
	switch($trunk_type):
	
	 case "origin":

	 $mysql_table = 'origin_trunks';
	
	 break;
	 
	 case "termination":
	 
	 $mysql_table = 'origin_trunks';
	 
	 break;
	endswitch;
	 
	 $this->db->where('carrier_id',$carrier_id);
	 
	 if($status != false)
	 {
		if($status == 'active')
		{
			$status = 1;
			$this->db->where('trunk_status',$status);
		}
	 }
	 
	 $query = $this->db->get($mysql_table);
	 
	 foreach($query->result_array() as $trunk)
	 {
		$trunks[] = $trunk;
	 }
	 
	 return $trunks;
}
	
public function change_trunk_status($trunk_id, $trunk_type, $status)
{
	switch($trunk_type):
	
	 case "origin":
	 
		// main table
		$data['trunk_status'] = $status;
		$mysql_table = "origin_trunks";		
		$this->db->where('id',$trunk_id)->update($mysql_table,$data);
				
	 break;
	 
	 case "termination":
	 
		// main table
		$data['trunk_status'] = $status;
		$mysql_table = "termination_trunks";		
		$this->db->where('id',$trunk_id)->update($mysql_table,$data);
		
		// final table
		unset($data);
		$data['termination_trunk_status'] = $status;
		$mysql_table = "relations_origin_termination";		
		$this->db->where('termination_trunk_id',$trunk_id)->update($mysql_table,$data);		
			 
	 break;
	 
	 endswitch;

	 // alternatively!!!
	$this->APPLICATION->load->model('app_mod_cron');
	$this->APPLICATION->app_mod_cron->update_final_routing_statuses();
	 
	return true;	 
}	

public function get_trunk_ip_addresses($trunk_id ,$trunk_type,$status = false)
{
	$addresses = array();
	
	switch($trunk_type):
	
	 case "origin":
	 
		$mysql_table = "origin_access_params";
	 
	 break;
	 
	 case "termination":
	 
		$mysql_table = "termination_access_params";
	 
	 break;
	
	endswitch;
	
	if($status == "all")
	{	
		$query = $this->db->where('trunk_id',$trunk_id)->order_by('creation_date','ASC')->get($mysql_table);		
	}
	else
	{
		$query = $this->db->where('trunk_id',$trunk_id)->where('status',1)->order_by('creation_date','ASC')->get($mysql_table);
	}
	
	foreach($query->result_array() as $address)
	{
		$addresses[] = $address;
	}
	return $addresses;
}
}